package com.hsav.kcxb.view;

import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;

import com.hsav.kcxb.R;
import com.hsav.kcxb.utils.BitmapUtil;

/**
 * Created by zhao on 2016/12/24.
 */

public class CircleView extends View {

    private final Paint mPaint;
    private int mType = 0;
    private static final int TYPE_CIRCLE = 0;
    private static final int TYPE_ROUND = 1;

    private Bitmap mBitmap;
    private int mRadius;
    private int mWidth;
    private int mHeight;


    public CircleView(Context context) {
        this(context, null);
    }

    public CircleView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CircleView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        Resources.Theme theme = context.getTheme();
        TypedArray typedArray = theme.obtainStyledAttributes(attrs, R.styleable.CustomImageView, defStyleAttr, 0);
        int count = typedArray.getIndexCount();
        for (int i = 0; i < count; i++) {
            int attr = typedArray.getIndex(i);
            switch (attr) {
                case R.styleable.CustomImageView_src:
                    mBitmap = BitmapFactory.decodeResource(getResources(), typedArray.getResourceId(attr, 0));
                    break;
                case R.styleable.CustomImageView_type:
                    mType = typedArray.getInt(attr, 0);
                    break;
                case R.styleable.CustomImageView_borderRadius:
                    int defaultSize = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 10f, getResources().getDisplayMetrics());
                    mRadius = typedArray.getDimensionPixelSize(attr, defaultSize);
                    break;
            }
        }
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        /**
         * Measure specification mode: The parent has determined an exact size
         * for the child. The child is going to be given those bounds regardless
         * of how big it wants to be.
         */
        if (widthMode == MeasureSpec.EXACTLY) {
            mWidth = widthSize;
        } else {
            int desireWidth = getPaddingLeft() + getPaddingRight() + mBitmap.getWidth();
//            Measure specification mode: The child can be as large as it wants up
//            * to the specified size.
            if (widthMode == MeasureSpec.AT_MOST) {
                mWidth = Math.min(widthSize, desireWidth);
            } else {
                /** UNSPECIFIED
                 * Measure specification mode: The parent has not imposed any constraint
                 * on the child. It can be whatever size it wants.
                 */
                mWidth = desireWidth;
            }
        }

        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        if (heightMode == MeasureSpec.EXACTLY) {
            mHeight = heightSize;
        } else {
            int desireHeight = getPaddingBottom() + getPaddingTop() + mBitmap.getHeight();
            if (heightMode == MeasureSpec.AT_MOST) {
                mHeight = Math.min(desireHeight, heightSize);
            } else {
                mHeight = desireHeight;
            }
        }
        int size = Math.min(mWidth, mHeight);
        mBitmap = Bitmap.createScaledBitmap(mBitmap, size, size, false);
        mBitmap = BitmapUtil.createCircleImg(mPaint, mBitmap, size);
        setMeasuredDimension(size, size);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        switch (mType) {
            case TYPE_CIRCLE:
                canvas.drawBitmap(mBitmap, 0, 0, null);
                break;
            case TYPE_ROUND:
                break;
        }
    }
}
